import {START_FRIEND_CHAT,REFRESH_FRIEND_LIST, DELETE_SUCCESS, FETCH_MESSAGES_SUCCESS} from '../contants/ActionTypes';
import { SEND_MESSAGE,SET_IS_OPEN_CHAT,LOGIN_FETCH_SUCESS } from "../contants/ActionTypes";

const initialState = {
    friendList:[
        {
           nickname:"黄敏院长",
           uuid:"1212121",
           avatar:"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1547360359&di=7134fdb3dedab0550869f70efc85297a&imgtype=jpg&er=1&src=http%3A%2F%2Fwww.juimg.com%2Ftuku%2Fyulantu%2F140618%2F330788-14061PJ00990.jpg",
           gender:"female",
           id:0
        },
        {
           nickname:"程兴国老师",
           uuid:"1313131",
           avatar:"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1546765688265&di=e8097cf877b7780b99d4714a68f9644d&imgtype=0&src=http%3A%2F%2Fimg5.duitang.com%2Fuploads%2Fitem%2F201411%2F12%2F20141112014133_UXfum.jpeg",
           gender:"female",
           id:1
        },
        {
            nickname:"梁浩贤",
            uuid:"1212121",
            avatar:"https://gss0.bdstatic.com/94o3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=63ca9e2d3dd12f2eda08a6322eabbe07/c995d143ad4bd1136bd648c851afa40f4bfb0534.jpg",
            gender:"female",
            id:2 
        },
        {
            nickname:"谢海可",
            uuid:"1212121",
            avatar:"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1547360433&di=678ddeeda74d55c396d27cc667c9b138&imgtype=jpg&er=1&src=http%3A%2F%2Facg.dmnico.cn%2FUploads%2Fnews%2F2018-06-20%2F3ca2bhiwe0x.jpg",
            gender:"female",
            id:3 
        },
        {
            nickname:"谌小虎",
            uuid:"1212121",
            avatar:"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1546765733746&di=3beefb84db4cf347c8c8a7a2bb934912&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201511%2F30%2F20151130161840_KAXsz.jpeg",
            gender:"female",
            id:3 
        },
        {
            nickname:"郑道明",
            uuid:"1212121",
            avatar:"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1546765764962&di=4150d7e343cbc9f35e9d7dc57333688e&imgtype=0&src=http%3A%2F%2Fimg.mp.sohu.com%2Fq_mini%2Cc_zoom%2Cw_640%2Fupload%2F20170601%2Faba658847ed246dc963c41a51dfca194_th.jpg",
            gender:"female",
            id:3 
        },
    ],
    currChatFriendId:"bbbbbb",
    messages:[
        {
        fromWho:false,//true is from myself
        text:"hey",
        id:0
        },
        {
            fromWho:false,//true is from myself
            text:"hi",
            id:1
        },
        {
            fromWho:true,//true is from myself
            text:"hi",
            id:1
        }
    ],
    isOpenChat:false
}


export default function friend(state = initialState, action) {
    switch(action.type) {
        case START_FRIEND_CHAT:
           return {
              ...state,
              currChatFriendId:action.currChatFriendId
           }
        case REFRESH_FRIEND_LIST:
          return {
              ...state,
              friendList:action.friendList
        }
        case SEND_MESSAGE:
            console.log(state);
            return{
                ...state,
                messages:[...state.messages,
                    {
                        fromWho:true,
                        text:action.text,
                        id:3
                        //id:state.messages.reduce((maxId,message)=> {if(message) return Math.max(message.id,maxId)},-1)+1
                    }
                ]
            }
        case SET_IS_OPEN_CHAT:
            return{
                ...state,
                isOpenChat: action.isOpen
            }
        case LOGIN_FETCH_SUCESS:
            return{
                ...state
            }
        case DELETE_SUCCESS:
            return{
                ...state
            }
        case FETCH_MESSAGES_SUCCESS:
            return{
                ...state,
                messages:"Nope.",
                isOpenChat:true
            }
        default:
            return state;            
    }   

}
